﻿using Flow.Manage.Contracts;
using Framework.Code.FlowWork;
using Framework.Models;
using Framework.Models.Entities;
using Framework.Models.Request;
using Framework.Models.Response;
using Framework.Repository;
using Framework.Utility;
using Framework.Utility.Helper;
using Framework.Utility.ViewModels;
using System.Reflection;

namespace Flow.Manage.Service
{
    public class UserService : IUserContract
    {
        private readonly IUserRepository  _userRepository;
    

        public UserService(IUserRepository  userRepository)
        {
            _userRepository = userRepository;
        }



        public TData<List<Biz_UserResponse>> GetUserList()
        {
            TData<List<Biz_UserResponse>> tData = new TData<List<Biz_UserResponse>>();

            tData.data = _userRepository.EntitiesAsNoTracking.ToList().MapTo<List<Biz_UserResponse>>(); ;

            return tData;
        }
        public TData<Biz_UserResponse> FindUserById(string Id)
        {
            TData<Biz_UserResponse> tData = new TData<Biz_UserResponse>();
            tData.data = _userRepository.EntitiesAsNoTracking.Where(u => u.Id == Id).FirstOrDefault().MapTo<Biz_UserResponse>();

            return tData;
        }


        public TData AddUser(Biz_UserRequest param)
        {
            var entity = param.MapTo<Biz_User>();


            entity.Id = new Guid().ToString();
            entity.CreateTime = DateTime.Now;
            entity.CreateBy = "system";
            var count = _userRepository.Insert(entity);

            return count > 0 ? new TData(ResultTag.success) : new TData(ResultTag.fail);
        }

        public TData UpdateUser(Biz_UserRequest param)
        {
            var entity = param.MapTo<Biz_User>();
      
            entity.UpdateTime = DateTime.Now;
            entity.UpdateBy = "system";
            var count = _userRepository.Update(entity);

            return count > 0 ? new TData(ResultTag.success) : new TData(ResultTag.fail);
        }

        public TData DeleteUser(Biz_UserRequest param)
        {
            var entity = param.MapTo<Biz_User>();

            var count = _userRepository.Delete(entity);

            return count > 0 ? new TData(ResultTag.success) : new TData(ResultTag.fail);
        }
    }
}